import { defineStore } from 'pinia'
interface AuthState {
  /** 用户信息 */
  userInfo: Auth.UserInfo
  lqryInfo: object,
  /** 登陆获取到的账号id */
  openid: string
  /** 用户token */
  sessionKey: string
  /** 登录的加载状态 */
  loginLoading: boolean
}
export const useAuthStore = defineStore({
  id: 'AuthState',
  state: (): AuthState => ({
    userInfo: {
      userId: '',
      /** 用户头像 */
      avatarUrl: '',
      /** 用户名 */
      nickName: '',
      /** 用户角色状态 */
      status: '',
      /** 用户单位*/
      company: '',
      /** 用户手机号 */
      userMobile: '',
    },
    lqryInfo: {
      yjlddwTywysbm: "06",
      xfjyjgTywysbm: "f0507828385c49c4a776f07561abaabf",
      dwMc: "中大医院江北院区",
      dwJc: null,
      dwDz: "健民路211号",
      dwJyqk: null,
      yjfwnrJyqk: null,
      lxrXm: "谢于生",
      lxrLxdh: "15851880263",
      czhm: null,
      zbdh: null,
      yjlddwlxdm: null,
      jysclx: null,
      dqjd: "118.747765",
      dqwd: "32.220625",
      dqgd: null,
      xzqhdm: null,
      zt: "1",
      bz: null,
      clxx: null,
      clzpUrl: null,
      zbxx: null,
      zbzpUrl: null,
      wzxx: null,
      wzzpUrl: null,
      jlzt: null,
      cjsj: "2021-05-06 15:18:30",
      gxsj: "2021-05-06 16:48:52"
    },
    openid: '',
    sessionKey: '',
    loginLoading: false,
  }),

  getters: {
    isLogin: (state) => {
      return Boolean(state.sessionKey)
    },
    getLqryInfo: (state) => {
      return state.lqryInfo
    },
    getOpenId: (state) => {
      return state.openid
    }
  },

  actions: {
    resetAuthStore() {
      this.$reset()
    },
    /**
     * 微信登录。后端返回的token存入
     * @param openid - 微信登录的id
     * @param sessionKey - 微信登录的 sessionKey
     */
    setAuthStore(userInfo: any,  openid: string, sessionKey: string) {
      Object.assign(this, { userInfo, openid, sessionKey})
    },

    setLqryInfo(lqryInfo:object) {
      // Object this.$state.lqryInfo = lqryInfo
      this.$state.lqryInfo = lqryInfo
      return true
    }

  },
})
